
var app = getApp();
var ddapi = require('../../utils/md5.js');
Page({

  data: {
    latitude: '',
    longitude: '',
    len: 1000,
    lookList: [],
    lookCount: 0,
    shopList: [],
  },

  onShow: function () {
    var that = this
    that.loadLookShop();
    that.loadLocation();
  },
  loadLookShop: function() {
    var that = this
    var lookShopList = [];
    var lookList = [];
    var count = 0;
    if (wx.getStorageSync('lookShopList')) {
      lookShopList = wx.getStorageSync('lookShopList');
    }
    
    for (var i = lookShopList.length - 1; i >= 0; i--) {
      var dic = lookShopList[i];
      if (dic.shop_id > 0) {
        if (count < 12) {
          var name = dic.shop_name;
          if (name.length > 7) {
            name = name.substr(0,7);
          }
          dic.shop_name = name;
          lookList.push(dic);
        }
        count++;
      }
     
    }
    console.log(123);
    console.log(lookList);
    that.setData({
      lookList: lookList,
      lookCount: lookList.length
    })
  },

  loadLocation: function() {
    var that = this
    wx.getLocation({
      type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        console.log(res)
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        });
        that.loadShop();
      }
    });
  },

  loadShop: function() {
    var that = this
    var center_jingwei = that.data.latitude + ',' + that.data.longitude;
    var hangye_id = 0;
    var length = that.data.len;
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.NewShop';
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(center_jingwei + hangye_id + length + openid + service + user_id);

    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.NewShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        center_jingwei: center_jingwei,
        hangye_id: hangye_id,
        length: length,
        openid: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var shop = res.data.data.info
        var shopList = []
        if (shop.length > 0) {
          for (var i = 0; i < shop.length; i++) {
            var dic = shop[i];
            var name = dic.shop_name;
            if (name.length > 7) {
              name = name.substr(0, 7);
            }
            dic.shop_name = name;
            shopList.push(dic);
          }
        }

        that.setData({
          shopList: shopList
        });
      }
    })
  },
  toHangyeShop: function(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var user_phone = wx.getStorageSync('userPhone');
    if (user_phone) {
      wx.navigateTo({
        url: "/pages/shop-detail/index?id=" + id
      })
    } else {
      var url = '/pages/login/login';
      wx.navigateTo({
        url: url
      })
    }
    that.getlod();
  },
  getlod: function () {
    var that = this;
    var service = 'User.CheckLand';
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var sign = ddapi.md5(open_id + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.CheckLand',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info == 0) {
            app.globalData.user_phone = '';
            wx.clearStorage();
            wx.navigateTo({
              url: "/pages/login/login"
            })
          }
        }
      }
    })
  },

})